package com.br.aiops.alert.config.db;

import com.br.aiops.alert.config.Receiver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ReceiverService {

    @Autowired
    private ReceiverConfigRepository receiverConfigRepository;

    @Autowired
    private ReceiverGroupUsersRepository receiverGroupUsersRepository;

    public List<Receiver> getReceiverByPerson() {
        return receiverConfigRepository.findAll().stream()
                .map(receiverConfigDTO -> Receiver.builder()
                        .casId(receiverConfigDTO.getCasId())
                        .ruleId(receiverConfigDTO.getRuleId())
                        .email(receiverConfigDTO.getEmail())
                        .phone(receiverConfigDTO.getPhone())
                        .owner(receiverConfigDTO.getOwner())
                        .build()).collect(Collectors.toList());
    }

    public List<Receiver> getReceiverByGroup() {
        return receiverGroupUsersRepository.findAll().stream()
                .map(receiverConfigDTO -> Receiver.builder()
                        .casId(receiverConfigDTO.getCasId())
                        .ruleId(receiverConfigDTO.getRuleId())
                        .groupId(receiverConfigDTO.getGroupId())
                        .email(receiverConfigDTO.getEmail())
                        .phone(receiverConfigDTO.getPhone())
                        .owner(receiverConfigDTO.getOwner())
                        .build()).collect(Collectors.toList());
    }
}
